package com.gemalto.Kryptik.Gui;


import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import com.gemalto.Kryptik.Util.*;
import com.gemalto.Kryptik.Gui.*;
import com.gemalto.Kryptik.Algo.*;
import com.gemalto.Kryptik.*;
import com.gemalto.Kryptik.MacroCamera.*;

public class MainTab extends JPanel implements ActionListener {


	private SearchableTextArea plainText, encryptedText;
	private CountedTextField signText;
	private JPanel buttonPanel;
	private JButton encryptButton, decryptButton, signButton;	
	private AlgoManager algoManager, macAlgoManager;

	public MainTab(AlgoManager algoManager, AlgoManager macAlgoManager){

		this.algoManager=algoManager;
		this.macAlgoManager=macAlgoManager;
		

        	setLayout(new javax.swing.BoxLayout(this, 1));
        	setBorder(new javax.swing.border.EmptyBorder(new java.awt.Insets(1, 1, 0, 0)));

		plainText=new SearchableTextArea(NewGui.getCounter());
          	plainText.setLineWrap(true);
          	plainText.setBorder(new javax.swing.border.LineBorder(java.awt.Color.black));
          	plainText.setRows(5);
          	plainText.setFont(new java.awt.Font ("Courier New", 0, 12));
	        plainText.setDocument(new HexDocument(0));
		plainText.getDocument().putProperty("name","plainText");
		plainText.getDocument().addDocumentListener(MacroCamera.getInstance());
		plainText.addKeyListener(MacroCamera.getInstance());

		encryptedText=new SearchableTextArea(NewGui.getCounter());
          	encryptedText.setLineWrap(true);
          	encryptedText.setBorder(new javax.swing.border.LineBorder(java.awt.Color.black));
          	encryptedText.setRows(5);
          	encryptedText.setFont(new java.awt.Font ("Courier New", 0, 12));
          	encryptedText.setDocument(new HexDocument(0));
		encryptedText.getDocument().putProperty("name","encryptedText");
		encryptedText.getDocument().addDocumentListener(MacroCamera.getInstance());
		encryptedText.addKeyListener(MacroCamera.getInstance());

		buttonPanel = new JPanel();
        	buttonPanel.setLayout(new javax.swing.BoxLayout(buttonPanel, 0));
        	buttonPanel.setBorder(new javax.swing.border.EmptyBorder(new java.awt.Insets(2, 0, 0, 0)));

		encryptButton = new JButton(Util.getTranslation("ENCRYPT_BUTTON"));
		encryptButton.setMargin(new Insets(0,0,0,0));
		encryptButton.setActionCommand("ENCRYPT");
		encryptButton.addActionListener(this);
		encryptButton.addActionListener(MacroCamera.getInstance());
		encryptButton.setMaximumSize(new Dimension(70, 25));
		encryptButton.setPreferredSize(new Dimension(70, 25));
		encryptButton.setMinimumSize(new Dimension(70, 25));
		encryptButton.setMnemonic(Util.getMnemonicChar("ENCRYPT_BUTTON"));


		decryptButton = new JButton(Util.getTranslation("DECRYPT_BUTTON"));
		decryptButton.setMargin(new Insets(0,0,0,0));
		decryptButton.setActionCommand("DECRYPT");
		decryptButton.addActionListener(this);
		decryptButton.addActionListener(MacroCamera.getInstance());
		decryptButton.setMaximumSize(new Dimension(70, 25));
		decryptButton.setPreferredSize(new Dimension(70, 25));
		decryptButton.setMinimumSize(new Dimension(70, 25));
		decryptButton.setMnemonic(Util.getMnemonicChar("DECRYPT_BUTTON"));
		
		signButton = new JButton(Util.getTranslation("SIGN_BUTTON"));
		signButton.setMargin(new Insets(0,0,0,0));
		signButton.setActionCommand("SIGN");
		signButton.addActionListener(this);
		signButton.addActionListener(MacroCamera.getInstance());
		signButton.setMaximumSize(new Dimension(65, 25));
		signButton.setPreferredSize(new Dimension(65, 25));
		signButton.setMinimumSize(new Dimension(65, 25));
		signButton.setMnemonic(Util.getMnemonicChar("SIGN_BUTTON"));


		signText = new CountedTextField(0, NewGui.getCounter());
		signText.setMaximumSize(new Dimension(Short.MAX_VALUE, 25));
          	signText.setFont(new java.awt.Font ("Courier New", 0, 12));
//		signText.setPreferredSize(new Dimension(30, 25));
//		signText.setMinimumSize(new Dimension(30, 25));
		signText.getDocument().addDocumentListener(MacroCamera.getInstance());
		signText.addKeyListener(MacroCamera.getInstance());
		signText.getDocument().putProperty("name","signText");

		buttonPanel.add(encryptButton);
		buttonPanel.add(decryptButton);
		buttonPanel.add(signButton);
//		buttonPanel.add(signText);
		

		Beanshell.setVariable("plainText", plainText);
		Beanshell.setVariable("encryptedText", encryptedText);
		Beanshell.setVariable("signText", signText);
		Beanshell.setVariable("encryptButton", encryptButton);
		Beanshell.setVariable("decryptButton", decryptButton);
		Beanshell.setVariable("signButton", signButton);
		Beanshell.setVariable("buttonPanel",buttonPanel);

		add(plainText);
		add(encryptedText);
		add(buttonPanel);
	}



	public void actionPerformed(ActionEvent e){
		String command=e.getActionCommand();
		String text, result, resultText;
		boolean hasSelection=true;

		if ( algoManager!=null){
			try {
					if (command.equals("ENCRYPT")) {
						text=plainText.getSelectedText();
						if ( text==null ) {
							text=plainText.getText();
							hasSelection=false;
						}

							result=((ISymetricCipher)algoManager.getActiveAlgo()).doEncryption(text);

						if (hasSelection){
							resultText=plainText.getText().substring(0,plainText.getSelectionStart())+
								result +
								plainText.getText().substring(plainText.getSelectionEnd());
						
						} else {
							resultText = result;
						}			
						encryptedText.setText(resultText);

					} else if (command.equals("DECRYPT")) {

						text=encryptedText.getSelectedText();
						if ( text==null ) {
							text=encryptedText.getText();
							hasSelection=false;
						}

						result=((ISymetricCipher)algoManager.getActiveAlgo()).doDecryption(text);

						if (hasSelection){
							resultText=encryptedText.getText().substring(0,encryptedText.getSelectionStart())+
								result +
								encryptedText.getText().substring(encryptedText.getSelectionEnd());
						
						} else {
							resultText = result;
						}			
						plainText.setText(resultText);


					} else if (command.equals("SIGN")) {
						text=plainText.getSelectedText();
						if ( text==null ) {
							text=plainText.getText();
							hasSelection=false;
						}

						result = ((ISignature)macAlgoManager.getActiveAlgo()).doSignature(text);

                                                //signText.setText(result);
                                                encryptedText.setText(result);
					}
                } catch (AlgoException ex) {
					Messenger.error(ex.getMessage());
					System.err.println(ex.getMessage());
				}

		}
	}


	public SearchableTextArea getPlainText(){
		return plainText;
	}

	public SearchableTextArea getEncryptedText(){
		return encryptedText;
	}

	public CountedTextField getSignText(){
		return signText;
	}

}


